package com.gitee.wsl.compose.ui.swiperefresh.sample.state

import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.gitee.wsl.compose.ui.swiperefresh.state.ActionStatus
import timber.log.Timber
import kotlin.math.absoluteValue

sealed class Action(
    private val _offsetY: State<Float>,
) {
    var componentStatus: ActionStatus by mutableStateOf(ActionStatus.IDLE)
        private set
    var hasMoreData: Boolean by mutableStateOf(true)
        internal set

    internal var triggerDistancePx: Float = 0f

    abstract val dragProgress: Float


    val offsetY: Float
        get() = _offsetY.value

    override fun toString(): String {
        return "ActionState(offsetY=$offsetY, componentStatus=$componentStatus, hasMoreData=$hasMoreData, triggerDistancePx=$triggerDistancePx)"
    }

    class Refreshing internal constructor(offsetY: State<Float>) : Action(offsetY) {

        override val dragProgress: Float
            get() = if (offsetY <= 0) 0f else (offsetY / triggerDistancePx).absoluteValue

    }

    class LoadingMore internal constructor(offsetY: State<Float>) : Action(offsetY) {
        override val dragProgress: Float
            get() = if (offsetY >= 0) 0f else (offsetY / triggerDistancePx).absoluteValue

    }

    internal fun updateComponentStatus(status: ActionStatus) {
        if (status == componentStatus) {
            return
        }
        Timber.d("${this.javaClass.simpleName} updateComponentStatus:$componentStatus -> $status ")
        componentStatus = status
    }

}

